import CreateDatabase from "../components/databaseInfo.vue"
import {
	SqlBuilder
} from './SqlBuilder'
import { metadataCovert, metaDefaultConfg } from "./constant"
import {ScriptApi} from "../api/scriptApi"
class ResouceFunctionType {
	constructor(opt, params) {
		this.opt = opt;
		this.params = params
		this.node = params.node;
		this.messageBox = opt.messageBox;
		this.sqlBuilder = new SqlBuilder();
		this.scriptApi = new ScriptApi(opt.request,opt.message)
	}

	getMetadata() {
		const {
			connId,
			catalog,
			tableName
		} = this.node.data.properties

		const rowCovert = (d, row) => {
			d.name = row.ROUTINE_NAME;
			d.id = `${connId}_${catalog}_${d.name}`;
			d.type = 'FUNCTION';
			d.properties.tableName = d.name;
			d.leaf = true;
	
		}
		return new Promise((resolve, reject) => {
		  const executeParams = {
			script: this.sqlBuilder.selectFunctions(catalog)
			,
        noPage:true
		  };
		  const params = {
			connId: this.node.data.properties.connId,
			executeParams: executeParams
		  };
		  this.scriptApi.singleExecute(params).then(res => {
			resolve(metadataCovert(this.opt, rowCovert, res, connId,catalog,tableName))
		  }).catch(e => {
			reject(e)
		  })
		})
	  }


	getContextMenus() {
		let menus = [];

		menus.push({
			label: "新建函数",
			dividedTop: true,
			click: (event, item) => {
				
				return true;
			},
		})
		return menus
	}
}

export {
	ResouceFunctionType
}
